package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ShareView
* */
class ShareView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.SQUARE
            strokeJoin = Paint.Join.MITER
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_BA340B)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.SQUARE
            strokeJoin = Paint.Join.MITER
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_BA340B)
        }
    }


    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            val mStrokeWidth = (cx / 10).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth
            mPaint1.strokeWidth = mStrokeWidth

            mPath.apply {
                reset()
                moveTo(cx * 11 / 12, -cy / 4)
                lineTo(cx * 11 / 12, cy * 11 / 12)
                lineTo(-cx * 11 / 12, cy * 11 / 12)
                lineTo(-cx * 11 / 12, -cy * 11 / 12)
                lineTo(cx / 4, -cy * 11 / 12)
            }
            drawPath(mPath, mPaint0)

            mPath.apply {
                reset()
                moveTo(cx * 7 / 12, -cy * 11 / 12)
                lineTo(cx * 11 / 12, -cy * 11 / 12)
                lineTo(cx * 11 / 12, -cy * 7 / 12)
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()
                moveTo(cx * 11 / 12 - mStrokeWidth / 2, -cy * 11 / 12 + mStrokeWidth / 2)
                lineTo(cx / 3, -cy / 3)
            }
            drawPath(mPath, mPaint1)

            restore()
        }
    }
}